//
// Created by Semir on 2020/4/26.
//

#include <iostream>
#include <deque>
#include <string>
#include <map>
#include <cmath>

using namespace std;

int n, k;
long long x;

int findLayer(long long x) {
    if (x >= pow(2, 60)) {
        return -1;
    }
    int low = 1;
    int high = 60;
    while (low <= high) {
        int mid = (low + high) >> 1;
        if (x < pow(2, mid - 1)) {
            high = mid - 1;
        } else if (x >= pow(2, mid) - 1) {
            low = mid + 1;
        } else {
            return mid;
        }
    }
    return -1;
}


int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> x >> k;
        int layer = findLayer(x);
        if (layer < 0 || k < 1 || k >= layer) {
            cout << "-1" << endl;
            continue;
        }
        for (int j = layer - k; j > 0; j--) {
            x = x >> 1;
        }
        cout << x << endl;
    }
    return 0;
}